﻿<#
    1. 本脚本使用方式，在windows powershell中运行。ps: 右键选择powershell运行即可
    2. 本脚本的case场景：
        服务端性能/全屏屏幕接口10次
#>

<#
屏幕截图命令
adb shell screencap -p /sdcard/screenshot.png
adb pull /sdcard/screenshot.png D:/screenshot.png


响应时间监测用log日志

adb logcat threadtime > xxx.log 本地日志
#>

  
chcp 65001

# - - - - - - - - - - - - constant - - - - - - - - - - - - - - - -
$app_name = "com.dayou.map/com.dayou.map.MainActivity"

$log_file = "./全屏屏幕接口.log"

$local_png_path = "./服务端性能"

# 循环次数
$ctimes = 10

# - - - - - - - - - - - - parameter - - - - - - - - - - - - - - - -
# 全屏
$full_screen = 1880, 310
# - - - - - - - - - - basic function - - - - - - - - - - - - - -
# 重新定义print内容
function print($_content) {
    $cur_time = Get-Date -Format "%y-%M-%d %h:%m:%s"
    Write-Host $cur_time $_content
    $all_content = "$cur_time, $_content"
    Write-Output  $all_content >> $log_file
}
# - - - - - - - - - - basic function - - - - - - - - - - - - - -

# 保存图片
function save_screenshot($file_path, $file_name) 
{
    <#
        @params: file_path 表示保存在android中的存储位置
        @params: file_name 表示图片保存的存储的名称
    #>
    $adb_path =  $(adb -s $device_sn shell ls -l $file_path).count
    if ($adb_path -eq 0){
        print "Exception: 传递文件的路径不存在"
        return 0
    }
    if ($file_path.endswith("/")) 
    {
        $file_abs_path = $file_path + $file_name
    }
    else{
        $file_abs_path = $file_path + "/" + $file_name
    } 
return 1
}

# 本地文件夹检查与创建
function local_dir_check($dir_path)
{
    if (Test-Path $dir_path) {
        return 1
    }
    else 
    {
        New-Object $dir_path -TypeName directory
        return 1
     }
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# - - - - - - - - - - affair function - - - - - - - - - - - - - -
# 检查adb是否正常安装
function check_adb_is_installed(){
    print "正在检测Adb命令工具是否安装中..."
    $adb_result=adb version
    if (-not $adb_result){
        Throw "Adb 电脑端没有找到安装路径，请检查 
        @1是否安装Adb工具，下载地址：https://dl.google.com/android/repository/platform-tools-latest-windows.zip 
        @2 系统环境变量是否正确添加
        "
    } else {
        if($adb_result.Count -gt 1 -and $adb_result[2].contains("Installed")) {
            $adb_path=$adb_result[2] -split "as"
            print @("已检查到Adb工具本地已正常安装！路径为-》", $adb_path[-1].Trim())
        } 
    }
}

# 查看adb设备是否连接上
function adb_service_connected() {
    print "正在查验车机设备是否连接上..."
    $services = adb devices | select -Skip 1
    if($services.Count -gt 1){
        for($i=0;$i -lt $services.Count - 1; $i+=1) {
            $sn=$services[$i].split()[0].trim()
            print @("设备连接正常，设为sn号为：",$sn)
        }
    } else {
        Throw "设备没有正常接入，请检查安卓系统是否开启了adb调试，步骤为：设置-》关于本机-》软件版本(连续点击5次)-》ADB模式切换(点击1次)"
    }
    # 返回值
    $sn
}

# 用于判断开始行驶的确认弹窗
function bar_window_judge()
{
    $windows = adb -s $device_sn shell dumpsys window | Select-String "Window #" -CaseSensitive | findstr "dayou"
    $_result = 1
    if ($windows.count -eq 2){$_result = 0}
    # 返回结果
    return $_result
}

# 启动开始行驶功能
function full_screen_test($params) {
    $times = $params

    # 设置站点下拉框的选项
    $_list_index = 0
    # 循环times次
    for($_i=0;$_i -lt $times; $_i+=1) {
        print "- -[正在第$_i次运行]- -"
        # 鼠标点击屏幕中心让屏幕地图窗口激活
        adb -s $device_sn shell input mouse tap 960 360

        # 点击全屏屏幕接口
        adb -s $device_sn shell input tap $full_screen
        local_dir_check $local_png_path
        save_screenshot $android_png_path $("第" + $_i +"次的全屏图")
    }
}


# - - - - - - - - - 初始化相关操作 - - - - - - - - - - - - - - -
# 检查adb工具
check_adb_is_installed
# 安卓设备是否正常连接
$global:device_sn = adb_service_connected

# 以root身份操作adb
adb root
# 初始化操作
adb -s $device_sn shell am force-stop $app_name.Split("/")[0]

# 获取屏幕分辨率
$size_string = $(adb -s $device_sn shell wm size).split()[2]
$global:screen_size_x = [int]$size_string.Split("x")[0]
$global:screen_size_y = [int]$size_string.Split("x")[1]
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

# 启动APP 
adb -s $device_sn shell am start -n $app_name
# 等待界面响应
Start-Sleep 15


# 点击全屏10次
full_screen_test($ctimes)

# 停止APP
adb -s $device_sn shell am force-stop $app_name.Split("/")[0]
